package com.jiyun.service;

import com.jiyun.mapper.UserMapper;
import com.jiyun.pojo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class UserService {
    @Autowired(required = false)
    private UserMapper userMapper;

    /*查询所有
    * 普通用户列表，要求功能点有：
        1、根据用户名、昵称、手机号、注册时间模糊查询；
        2、禁止和解禁用户，禁用后，用户不能进行发布信息操作，解禁之后，可以正常发布
        3、删除用户功能，删除数据之后，要求立即将用户踢出系统
    * */
    public List<User> getAllUser(User user) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("roleid",2);
        if (!StringUtils.isEmpty(user.getUsername())) {
            criteria.andLike("username","%"+user.getUsername()+"%");
        }
        if (!StringUtils.isEmpty(user.getNickname())) {
            criteria.andLike("nickname","%"+user.getNickname()+"%");
        }
        if (!StringUtils.isEmpty(user.getPhone())) {
            criteria.andEqualTo("phone",user.getPhone());
        }

        if (!StringUtils.isEmpty(user.getRegistrationtime()) && !StringUtils.isEmpty(user.getLogintime())) {
            criteria.andBetween("registrationtime", user.getRegistrationtime(), user.getLogintime());
        }
        if (!StringUtils.isEmpty(user.getRegistrationtime()) && StringUtils.isEmpty(user.getLogintime())) {
            criteria.andGreaterThanOrEqualTo("registrationtime",user.getRegistrationtime());
        }
        if (StringUtils.isEmpty(user.getRegistrationtime()) && !StringUtils.isEmpty(user.getLogintime())) {
            criteria.andLessThanOrEqualTo("registrationtime",user.getLogintime());
        }
        List<User> users = userMapper.selectByExample(example);
        return users;
    }

    /*
    * 1、根据用户名、昵称、手机号、注册时间模糊查询；
    2、禁止和解禁跑腿员，禁用后，跑腿员不能进行接单操作，解禁之后，可以正常接单
    3、删除跑腿员功能，删除数据之后，要求立即将接单员踢出系统
    * */
    public List<User> getAllpt(User user) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("roleid",3);
        if (!StringUtils.isEmpty(user.getUsername())) {
            criteria.andLike("username","%"+user.getUsername()+"%");
        }
        if (!StringUtils.isEmpty(user.getNickname())) {
            criteria.andLike("nickname","%"+user.getNickname()+"%");
        }
        if (!StringUtils.isEmpty(user.getPhone())) {
            criteria.andEqualTo("phone",user.getPhone());
        }
        if (!StringUtils.isEmpty(user.getRegistrationtime()) && !StringUtils.isEmpty(user.getLogintime())) {
            criteria.andBetween("registrationtime", user.getRegistrationtime(), user.getLogintime());
        }
        if (!StringUtils.isEmpty(user.getRegistrationtime()) && StringUtils.isEmpty(user.getLogintime())) {
            criteria.andGreaterThanOrEqualTo("registrationtime",user.getRegistrationtime());
        }
        if (StringUtils.isEmpty(user.getRegistrationtime()) && !StringUtils.isEmpty(user.getLogintime())) {
            criteria.andLessThanOrEqualTo("registrationtime",user.getLogintime());
        }
        List<User> users = userMapper.selectByExample(example);
        return users;
    }

    public void jinzhi(Integer userid) {
        userMapper.jinzhi(userid);
    }

    public void jiejin(Integer userid) {
        userMapper.jiejin(userid);
    }

    public void deleteById(Integer userid) {
        userMapper.deleteByPrimaryKey(userid);
    }

    public User getUserByUser(User user) {
        String username = user.getUsername();
        String password = user.getPassword();
        String phone = user.getPhone();
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(username)){
            criteria.andEqualTo("username", username);
        }
        if (!StringUtils.isEmpty(password)){
            criteria.andEqualTo("password", password);
        }
        if (!StringUtils.isEmpty(phone)){
            criteria.andEqualTo("phone", phone);
        }
        List<User> users = userMapper.selectByExample(example);
        User user1 = users.get(0);
        return user1;
    }
    public User findUserByid(Integer userid) {
        User user = userMapper.selectByPrimaryKey(userid);
        System.out.println("----------------User="+user);
        return user;
    }

    public void updateUserpasswordAndphone(User user) {
        userMapper.updateByPrimaryKeySelective(user);
    }
}
